package com.zc.controller;

import com.zc.component.EntityFile;
import com.zc.entity.Bug;
import com.zc.entity.Fileer;
import com.zc.entity.Needs;
import com.zc.mapper.BugMapper;
import com.zc.mapper.ItemMapper;
import com.zc.mapper.NeedsMapper;
import com.zc.mapper.UserMapper;
import com.zc.util.FileUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class BugController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    EntityFile entityFile;
    @Autowired
    BugMapper bugMapper;
    @Autowired
    ItemMapper itemMapper;
    @GetMapping("doaddbug")
    public String doaddbug(Model model, HttpSession session)
    {
        int teamid = (int) session.getAttribute("teamid");
        List userlist=userMapper.selectByTeamId(teamid);
        model.addAttribute("userlist",userlist);
        return "bug/addbug";
    }
    @ResponseBody
    @PostMapping("/addbug")
   // public  String needadd(Bug bug, HttpSession session)
    public  String needadd(@RequestParam("fileName") MultipartFile file, Bug bug,
                           HttpSession session)
    {

        int teamid = (int) session.getAttribute("teamid");
        int newid = (int) session.getAttribute("userid");
        int itemid=itemMapper.selectByTeamId(teamid).getId();
        String exts = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);

        String fileName =UUID.randomUUID().toString()+"."+exts;

        bug.setBugjpg(fileName);
        bug.setItemid(itemid);
        bug.setUpid(newid);
        bug.setStatus("1");
        bug.setNewdate(new Date());

        if (file.isEmpty()) {
            return "false";
        }
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" +size);

        String path = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
        File dest = new File(path +"/"+teamid+"/"+ file.getOriginalFilename());
        File newFile=new File(path +"/"+teamid+"/"+ fileName);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            bugMapper.insertSelective(bug);
            file.transferTo(dest); //保存文件
            try
            {
                FileInputStream fis = new FileInputStream(dest);//创建输入流对象
                FileOutputStream fos = new FileOutputStream(newFile); //创建输出流对象
                byte datas[] = new byte[1024*8];//创建搬运工具
                int len = 0;//创建长度
                while((len = fis.read(datas))!=-1)//循环读取数据
                {
                    fos.write(datas,0,len);
                }
                fis.close();//释放资源
                fis.close();//释放资源
                FileUtil.deleteDir(path +"/"+teamid+"/"+ file.getOriginalFilename());
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return "true";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }

    }
    @ResponseBody
    @GetMapping("/buglist/{status}")
    public  List needslist(@PathVariable("status") Integer status, HttpSession session)
    {
        int teamid = (int) session.getAttribute("teamid");
        int itemid=itemMapper.selectByTeamId(teamid).getId();
        List<Bug> bugs=null;
        if(status==0)
        {
            bugs=bugMapper.selectByitemId(itemid);
        }else {
            bugs=bugMapper.selectByitemIdAndGrade(itemid,status);
        }

        return bugs;
    }
    @GetMapping("/bugweihu")
    public String needweihu(@Param("id")int id,
                            Model model,HttpSession session){
        int teamid = (int) session.getAttribute("teamid");
        List userlist=userMapper.selectByTeamId(teamid);
        model.addAttribute("userlist",userlist);
        Bug need=bugMapper.selectByPrimaryKey(id);
        model.addAttribute("need",need);
        return "bug/bugweihu";
    }
    @PutMapping("/bugweihu")
    public String userUpdate1(Bug bug)
    {
        bug.setNewdate(new Date());
        bugMapper.updateByPrimaryKeySelective(bug);
        return "bug/buglist";
    }
    @ResponseBody
    @GetMapping("/guanbibug/{id}")
    public  String guanbineed(@PathVariable("id") Integer id, HttpSession session)
    {
        Bug bug=new Bug();
        bug.setId(id);
        bug.setStatus("4");
        bugMapper.updateByPrimaryKeySelective(bug);
        return "SUCCESS";
    }
    @GetMapping("/chakanbug")
    public  String chakanbug(@RequestParam("id") Integer id, Model model , HttpSession session)
    {
        int teamid = (int) session.getAttribute("teamid");
        Bug bug = bugMapper.selectByPrimaryKey(id);
        String path= "./"+teamid+"/"+ bug.getBugjpg();
        model.addAttribute("path",path);
        return "bug/chakanbug";
    }
    @ResponseBody
    @GetMapping("/kfbuglist/{downid}")
    public  List kfneedslist(@PathVariable("downid") Integer downid, HttpSession session)
    {
        int teamid = (int) session.getAttribute("teamid");
        int userid = (int) session.getAttribute("userid");
        int itemid=itemMapper.selectByTeamId(teamid).getId();
        List<Bug> bugs=null;
        if(downid==0)
        {
            bugs=bugMapper.selectByitemId(itemid);
        }else {
            bugs=bugMapper.selectByitemIdAnddownid(itemid,userid);
        }

        return bugs;
    }
    @ResponseBody
    @GetMapping("/qrbug/{id}")
    public  String qrbug(@PathVariable("id") Integer id, HttpSession session)
    {
        Bug bug=new Bug();
        bug.setId(id);
        bug.setStatus("2");
        bugMapper.updateByPrimaryKeySelective(bug);
        return "SUCCESS";
    }
    @ResponseBody
    @GetMapping("/wcbug/{id}")
    public  String wcbug(@PathVariable("id") Integer id, HttpSession session)
    {
        Bug bug=new Bug();
        bug.setId(id);
        bug.setStatus("3");
        bug.setEnddate(new Date());
        bugMapper.updateByPrimaryKeySelective(bug);
        return "SUCCESS";
    }
}
